package com.naiterui.ehp.bs.doctor.dao.impl;

import com.naiterui.ehp.bp.domain.Login;
import com.naiterui.ehp.bs.doctor.dao.ILoginDao;
import com.naiterui.ehp.bs.doctor.repository.ILoginRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class AdLoginDaoImpl implements ILoginDao {

    @Autowired
    private ILoginRepository loginRepository;

    @Override
    public Login get(Long id) {
        return loginRepository.get(id);
    }

    @Override
    public boolean findCountByPhoneNum(String phoneNum) {
        long count = loginRepository.countByPhoneNum(phoneNum);
        return count != 0;
    }

    @Override
    public Long saveLogin(Login login) {
        login = loginRepository.save(login);
        return login.getId();
    }

    @Override
    public Login findLoginByPhoneNum(String phoneNum) {
        List<Login> list = loginRepository.findByPhoneNum(phoneNum);
        if (list != null && list.size() == 1) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public void updateLogin(Login login) {
        loginRepository.update(login);
    }

    @Override
    public boolean findExsitSaltRecomm(String salt, String recommCode) {

        if (salt == null || "".equals(salt)) {
            return true;
        }

        if (StringUtils.isEmpty(recommCode)) {
            return true;
        }

        long count = loginRepository.countBySaltOrRecommCode(salt, recommCode);
        return count != 0;
    }

    @Override
    public Login findLoginById(Long id) {
        return loginRepository.get(id);
    }

}
